package cn.com.nwpu.visual_system.tool;

import cn.com.nwpu.visual_system.component.InitConfig;
import cn.com.nwpu.visual_system.entity.ModelPredictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title ExecutePython
 * @description TODO 执行python脚本
 * @date 2024/5/18 19:56
 */
public class ExecutePython {

    public void train() {
        GetUrl getUrl = new GetUrl();
        String pythonScriptPath = getUrl.getPythonUrl(InitConfig.trainName);   // 根据文件名，找到对应python的路径
        String resourcesUrl = getUrl.getPythonUrl();
        ProcessBuilder pythonBuilder = new ProcessBuilder(InitConfig.evnPath, pythonScriptPath, InitConfig.db_data_size, InitConfig.db_data_index, resourcesUrl);
        pythonBuilder.redirectErrorStream(true);
        try {
            Process pr = pythonBuilder.start();
            int code = pr.waitFor();
            // 获取python脚本输出信息
            InputStream inputStream = pr.getInputStream();
            // TODO python环境字符编码格式还没统一（已经解决，在python文件开头设置输入和输出的字符编码格式，不然传参会有问题）
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String inputLine;
            while ((inputLine = inputReader.readLine()) != null) {  // 如果有返回值
                System.out.println(inputLine);
            }

            // 获取python脚本错误信息
            InputStream errorStream = pr.getErrorStream();
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {  // 如果有错误
                System.out.println(errorLine);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public ModelPredictData predict(String predictData) {
        GetUrl getUrl = new GetUrl();
        String pythonScriptPath = getUrl.getPythonUrl(InitConfig.testName);   // 根据文件名，找到对应python的路径
        String resourcesUrl = getUrl.getPythonUrl();
        ProcessBuilder pythonBuilder = new ProcessBuilder(InitConfig.evnPath, pythonScriptPath, predictData, resourcesUrl);
        pythonBuilder.redirectErrorStream(true);
        ModelPredictData modelPredictData = new ModelPredictData();
        List<String> readList = new ArrayList<>();
        try {
            Process pr = pythonBuilder.start();
            int code = pr.waitFor();

            InputStream inputStream = pr.getInputStream();
            // TODO python环境字符编码格式还没统一（已经解决，在python文件开头设置输入和输出的字符编码格式，不然传参会有问题）
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String inputLine;

            while ((inputLine = inputReader.readLine()) != null) {  // 如果有返回值
                readList.add(inputLine.substring(1, inputLine.length() - 1));
            }


            modelPredictData.setTValue(readList.get(0));
            modelPredictData.setSPE(readList.get(1));
            modelPredictData.setInsertTime(LocalDateTime.now());

            // 获取python脚本错误信息
            InputStream errorStream = pr.getErrorStream();
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {  // 如果有错误
                System.out.println(errorLine);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        return modelPredictData;
    }
}
